package com.rock.upms.autocode.config;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.springframework.core.io.DefaultResourceLoader;
import com.google.common.collect.Maps;
import com.rock.common.util.PropertiesFileUtil;
import com.rock.common.util.StringUtil;

/**
 *代码生成器配置类
 * 
 * @author hotdog
 *
 */
public class AutoCfg {

	private static AutoCfg global = new AutoCfg();
	private static Map<String, String> map = Maps.newHashMap();
	public static String ipAddress = "";

	public static AutoCfg getInstance() {
		return global;
	}

	public static String getConfig(String key) {
		String value = (String) map.get(key);
		if (value == null) {
			value = PropertiesFileUtil.getInstance("generator").get(key);
			map.put(key, value != null ? value : "");
		}
		return value;
	}

	
	public static String getOraclTablespaceName() {
		return getConfig("oracle.tablespace.name");
	}

	public static String getSystemCode() {
		return getConfig("system.code");
	}
	public static String getDatabaseName() {
		return getConfig("database.name");
	}
	
	public static String getTablePrefix() {
		return getConfig("table.prefix");
	}
	public static String getSystemName() {
		return getConfig("system.name");
	}

	public static String getCodePath() {
		return getConfig("code.path");
	}

	public static String getDatabaseType() {
		return getConfig("database.type");
	}

	public static String getJdbcDriver() {
		return getConfig("generator.jdbc.driver");
	}

	public static String getJdbcUrl() {
		return getConfig("generator.jdbc.url");
	}

	public static String getJdbcUsername() {
		return getConfig("generator.jdbc.username");
	}

	public static String getJdbcPassword() {
		return getConfig("generator.jdbc.password");
	}

	public static Object getConst(String field) {
		try {
			return AutoCfg.class.getField(field).get(null);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException localIllegalArgumentException) {
		}
		return null;
	}

	public static String getProjectPath() {
		String projectPath = getConfig("projectPath");
		if (StringUtil.isNotBlank(projectPath)) {
			return projectPath;
		}
		try {
			File file = new DefaultResourceLoader().getResource("").getFile();
			if (file != null) {
				for (;;) {
					File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
					if ((f == null) || (f.exists())) {
						break;
					}
					if (file.getParentFile() == null) {
						break;
					}
					file = file.getParentFile();
				}
				projectPath = file.toString();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return projectPath;
	}

	public static void main(String[] args) {
		System.out.println(AutoCfg.getDatabaseType());
		System.out.println("------------------------------------------");
	}
}
